//
//  ViewController.m
//  UGoods
//
//  Created by dllo on 16/1/15.
//  Copyright © 2016年 蓝鸥科技.李贺. All rights reserved.
//

#import "ViewController.h"
#import "FindViewController.h"
#import "ActivityViewController.h"
#import "GatherViewController.h"
#import "ShoppingViewController.h"
#import "MyViewController.h"
#import "BaseNavigationController.h"
#import "NightMode.h"
#import "ConstantsDefine.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)dealloc
{
    // dealloc移除观察着
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

// 白天
- (void)setLightDayModel {
    
    self.tabBar.tintColor = [UIColor blackColor];
    self.tabBar.barTintColor = [UIColor whiteColor];
    
    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20 * HEIGHT)];
    
    statusBarView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:statusBarView];
    [statusBarView release];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
}

// 夜间
- (void)setNightDayModel {
    
    self.tabBar.tintColor = [UIColor whiteColor];
    
    self.tabBar.barTintColor = [UIColor blackColor];
    
    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20 * HEIGHT)];
    
    statusBarView.backgroundColor= [UIColor blackColor];
    
    [self.view addSubview:statusBarView];
    [statusBarView release];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    if ([NightMode nightModeSharedInstance].isNight) {
        [self setNightDayModel];
    } else {
        [self setLightDayModel];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setLightDayModel) name:@"day" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setNightDayModel) name:@"night" object:nil];
    
    // 发现页
    FindViewController *find = [[FindViewController alloc] init];
    BaseNavigationController *navi1 = [[BaseNavigationController alloc] initWithRootViewController:find];
    find.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"iconfont-faxian"] tag:1001];
     [navi1.navigationBar setTintColor:[UIColor blackColor]];
    
    // 活动页
    ActivityViewController *activi = [[ActivityViewController alloc] init];
    BaseNavigationController *navi2 = [[BaseNavigationController alloc] initWithRootViewController:activi];
    activi.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"活动" image:[UIImage imageNamed:@"iconfont-huodong"] tag:1002];
    
    // U集页
    GatherViewController *gather = [[GatherViewController alloc] init];
    BaseNavigationController *navi3 = [[BaseNavigationController alloc] initWithRootViewController:gather];
    gather.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"U集" image:[UIImage imageNamed:@"iconfont-taolun"] tag:1003];
    
    // 购物袋
//    ShoppingViewController *shopping = [[ShoppingViewController alloc] init];
//    BaseNavigationController *navi4 = [[BaseNavigationController alloc] initWithRootViewController:shopping];
//    shopping.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"购物袋" image:[UIImage imageNamed:@"iconfont-gouwu-3"] tag:1004];
    
    // 我的
    MyViewController *my = [[MyViewController alloc] init];
    BaseNavigationController *navi5 = [[BaseNavigationController alloc] initWithRootViewController:my];
    my.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"iconfont-wode-2"] tag:1005];
    
    
    
    self.viewControllers = @[navi1, navi2, navi3, navi5];
    
    [find release];
    [activi release];
    [gather release];
//    [shopping release];
    [my release];
    [navi1 release];
    [navi2 release];
    [navi3 release];
//    [navi4 release];
    [navi5 release];
    [ViewController registerLocalNotification:5];
}

//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
//    [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        
//        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"“U集品”，想给您发送推送通知" message:@"“通知”可能包括提醒，声音和图标标记。这些可在“设置中配置”" preferredStyle:UIAlertControllerStyleAlert];
//        
//        UIAlertAction *notAllowed = [UIAlertAction actionWithTitle:@"不允许" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//            
//            // 在不需要在推送时，可以取消推送
//            [ViewController cancelLocalNotificationWithKey:@"key"];
//        }];
//        
//        UIAlertAction *okey = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            
//            [ViewController registerLocalNotification:5];
//        }];
//        
//        [alert addAction:notAllowed];
//        [alert addAction:okey];
//        [self presentViewController:alert animated:YES completion:^{
//            
//        }];
//        
//
//    }
//    return self;
//}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"“U集品”，想给您发送推送通知" message:@"“通知”可能包括提醒，声音和图标标记。这些可在“设置中配置”" preferredStyle:UIAlertControllerStyleAlert];
    
            UIAlertAction *notAllowed = [UIAlertAction actionWithTitle:@"不允许" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    
                // 在不需要在推送时，可以取消推送
                [ViewController cancelLocalNotificationWithKey:@"key"];
            }];
    
            UIAlertAction *okey = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
                [ViewController registerLocalNotification:5];
            }];
    
            [alert addAction:notAllowed];
            [alert addAction:okey];
            [self presentViewController:alert animated:YES completion:^{
                
            }];

}

+ (void)registerLocalNotification:(NSInteger)alertTime {
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    // 设置触发通知时间
    NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];
    NSLog(@"fireDate:%@", fireDate);
    
    notification.fireDate = fireDate;
    
    // 时区
    notification.timeZone = [NSTimeZone defaultTimeZone];
    
    // 设置重复的间隔
    notification.repeatInterval = kCFCalendarUnitSecond;
    
    // 通知内容
    notification.alertBody = @"更多优质精品，尽在优集品";
    notification.applicationIconBadgeNumber = 1;
    
    // 通知被触发时播放的声音
    notification.soundName = UILocalNotificationDefaultSoundName;
    
    // 通知参数
    NSDictionary *userDic = [NSDictionary dictionaryWithObject:@"更多优质精品，尽在U集品" forKey:@"key"];
    notification.userInfo = userDic;
    
    // iOS8后，需要添加这个注册，才能得到授权
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        
        // 通知重复提示的单位，可以是天，周，月
        notification.repeatInterval = kCFCalendarUnitMinute;
    } else {
        // 通知重复提示的单位，可以是天，周，月
        notification.repeatInterval = kCFCalendarUnitMinute;
    }
    
    // 执行通知注册
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

// 取消某个本地通知
+ (void)cancelLocalNotificationWithKey:(NSString *)key {
    // 获取所有本地通知数组
    NSArray *localNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
    
    for (UILocalNotification *notification in localNotifications) {
        NSDictionary *userInfo = notification.userInfo;
        if (userInfo) {
            // 根据设置通知时指定的key来获取通知参数
            NSString *info = userInfo[key];
            
            // 如果找到需要取消的通知，则取消
            if (info != nil) {
                [[UIApplication sharedApplication] cancelLocalNotification:notification];
                break;
            }
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
